<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" @findClicked="findClicked" :formList="formList" @Launchform="Launchform" />

    <el-card shadow="never" style="margin-top: 15px" :body-style="{ padding: '15px' }">
      <div class="flex mt10 main">
        <div style="width: 30%">
          <span style="font-weight: 600">待汇总指令</span>
          <Table
            :isLoading="isLoading"
            @currentRow="currentRow3"
            @sortChange="HeaderList"
            :tableHeader="tableHeader"
            :tableData="QueryToBeSummarizedTableDate"
            :TableHeight="50"
          ></Table>
          <PageModule
            :PageData="{ total, buttonS: true, pageSizeShow: true }"
            @pageSizeChange="handleSizeChange"
            :layout="'prev, pager'"
            @pageChange="handleCurrentChange"
          />
        </div>

        <div style="width: 30%; margin-left: 15px; margin-top: 1px">
          <span style="font-weight: 600">汇总号查询</span>
          <Table
            @selectList="selectList1"
            selection
            :isLoading="isLoading1"
            @currentRow="currentRow1"
            :tableHeader="tableHeader2"
            :tableData="SummaryInvoiceQueryTableDate"
            :TableHeight="50"
          ></Table>
          <PageModule
            :PageData="{ total: totalList, buttonS: true, pageSizeShow: true }"
            :layout="'prev, pager'"
            @pageSizeChange="handleSizeChange1"
            @pageChange="handleCurrentChange1"
          />
        </div>

        <div style="margin-left: 15px; width: 40%">
          <span style="font-weight: 600">汇总材料查询</span>
          <Table
            :isLoading="isLoading2"
            @currentRow="currentRow2"
            :tableHeader="tableHeader3"
            :tableData="materialsQuerryTableDate"
            :TableHeight="50"
          ></Table>
        </div>
      </div>

      <div class="flex main">
        <div style="width: 30%">
          <span style="font-weight: 600">汇总资料查询</span>
          <Table :isLoading="isLoading3" :tableHeader="tableHeader4" :tableData="QyeryLotNoMethodsTableDate" :TableHeight="45"></Table>
        </div>

        <div style="width: 50%; margin-left: 15px">
          <span style="font-weight: 600">贴合子资料查询</span>
          <Table :isLoading="isLoading4" :tableHeader="tableHeader5" :tableData="FitSubMaterialQueryTableDateList" :TableHeight="45"></Table>
        </div>

        <div style="width: 20%; margin-left: 15px">
          <span style="font-weight: 600">汇总指令材料明细</span>
          <Table :isLoading="isLoading5" :tableHeader="tableHeader6" :tableData="FitSubMaterialQueryListDateList" :TableHeight="45"></Table>
        </div>
      </div>
    </el-card>

    <el-dialog v-model="dialogVisible5" title="删除指令查询" width="30%" class="dialog pb0">
      <el-button type="primary" size="default" @click="recoveryCommandMethods">恢复指令</el-button>

      <Table
        :isLoading="isLoading6"
        @currentRow="currentRow5"
        :tableHeader="tableHeader7"
        :tableData="QueryLotNoListTableDateList"
        :TableHeight="50"
      />
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import fileRequest from '@/utils/fileRequest'
import JMessage from '@/utils/JMessage'
export default defineComponent({
  name: 'FMaterialSummary'
})
</script>
<script lang="ts" setup>
import { i18n, Jzhcn } from '@/i18n'
import { ref, onMounted } from 'vue'
import {
  SummaryOperation,
  deleteSumm,
  recoveryCommand,
  QueryToBeSummarized,
  SummaryInvoiceQuery,
  materialsQuerry,
  QyeryLotNo,
  FitSubMaterialQuery,
  FitSubMaterialQueryList,
  deleteLotNoList,
  QueryLotNoList
} from '@/api/PurchasingDepartment/FactoryPurchasing/MaterialSummaryTl'
const isLoading = ref(true)
const isLoading1 = ref(true)
const isLoading2 = ref(true)
const isLoading3 = ref(true)
const isLoading4 = ref(true)
const isLoading5 = ref(true)
const isLoading6 = ref(true)

// 汇总作业
const SummaryOperationParms = ref({
  color: '',
  deliveryDate: '',
  lotFact: '',
  lotNo: '',
  lotType: '',
  style: '',
  sysDate: ''
})

const SummaryOperationMethods = async () => {
  if (SummaryOperationParms.value.lotNo === '') {
    JMessage.warning('请选择需要汇总的作业')
  } else {
    const result = await SummaryOperation(SummaryOperationParms.value)
    if (result.code === 200) {
      SummaryInvoiceQueryMethods()
      QueryToBeSummarizedMethods()
      SummaryOperationParms.value.lotNo = ''
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  }
}

// 汇总删除
const deleteSummParms = ref('')

const deleteSummMethods = async () => {
  const result = await deleteSumm(deleteSummParms.value)
  if (result.code === 200) {
    SummaryInvoiceQueryMethods()
    deleteSummParms.value = ''
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

const change11 = () => {
  if (deleteSummParms.value === '') {
    JMessage.warning('请选择需要删除的数据')
  } else {
    ElMessageBox.confirm('确认要删除该数据吗?')
      .then(async () => {
        deleteSummMethods()
      })
      .catch(() => {})
  }
}

// 恢复指令
const lotNoList1 = ref('')

const currentRow5 = (row: any) => {
  lotNoList1.value = row.lotNo
}

const recoveryCommandMethods = async () => {
  if (lotNoList1.value === '') {
    JMessage.warning('请选择需要恢复的指令')
  } else {
    const result = await recoveryCommand(lotNoList1.value)
    if (result.code === 200) {
      QueryToBeSummarizedMethods()
      lotNoList.value = ''
      JMessage.success(result.message)
    } else {
      JMessage.error(result.message)
    }
  }
}

// 删除指令查询
const dialogVisible5 = ref(false)

const QueryLotNoListTableDateList = ref([])

const QueryLotNoListMethods = async () => {
  isLoading6.value = true
  const result = await QueryLotNoList()
  QueryLotNoListTableDateList.value = result.map((item: any) => {
    return { lotNo: item }
  })
  isLoading6.value = false
}

// 总指令删除
const lotNoList = ref('')

const currentRow3 = (row: any) => {
  lotNoList.value = row.lotNo
  SummaryOperationParms.value.color = row.color
  SummaryOperationParms.value.deliveryDate = row.deliveryDate
  SummaryOperationParms.value.lotFact = row.lotFact
  SummaryOperationParms.value.lotNo = row.lotNo
  SummaryOperationParms.value.lotType = row.lotType
  SummaryOperationParms.value.style = row.style
  SummaryOperationParms.value.sysDate = row.sysDate
}

const change10 = () => {
  if (lotNoList.value === '') {
    JMessage.warning('请选择需要删除的数据')
  } else {
    ElMessageBox.confirm('确认要删除该数据吗?')
      .then(async () => {
        deleteLotNoListMehtods()
      })
      .catch(() => {})
  }
}

const deleteLotNoListMehtods = async () => {
  const result = await deleteLotNoList(lotNoList.value)
  if (result.code === 200) {
    QueryToBeSummarizedMethods()
    lotNoList.value = ''
    JMessage.success(result.message)
  } else {
    JMessage.error(result.message)
  }
}

// 汇总指令材料
const FitSubMaterialQueryListDateList = ref([])

const FitSubMaterialQueryListMethods = async () => {
  isLoading5.value = true
  const result = await FitSubMaterialQueryList(materialsQuerryPrams.value, mtlNoList.value, materialsQuerryLotNo.value)
  FitSubMaterialQueryListDateList.value = result
  isLoading5.value = false
}

// 贴合子材料查询
const mtlNoList = ref('')

const FitSubMaterialQueryTableDateList = ref([])

const FitSubMaterialQueryMethods = async () => {
  isLoading4.value = true
  const result = await FitSubMaterialQuery(materialsQuerryPrams.value, mtlNoList.value, purNoListParms.value.lotNo)
  FitSubMaterialQueryTableDateList.value = result
  isLoading4.value = false
}

const currentRow2 = (row: any) => {
  mtlNoList.value = row.mtlNo
  FitSubMaterialQueryMethods()
  FitSubMaterialQueryListMethods()
}

// 汇总指令查询
const QyeryLotNoMethodsTableDate = ref<any>([])

const QyeryLotNoMethods = async () => {
  isLoading3.value = true
  const result = await QyeryLotNo(materialsQuerryPrams.value, materialsQuerryLotNo.value)
  QyeryLotNoMethodsTableDate.value = [result]
  isLoading3.value = false
}

const currentRow1 = (row: any) => {
  const { sumNo } = row
  materialsQuerryPrams.value = sumNo
  deleteSummParms.value = sumNo
  materialsQuerryMethods()
  QyeryLotNoMethods()
}

// 汇总材料查询
const materialsQuerryPrams = ref('')

const materialsQuerryLotNo = ref('')

const materialsQuerryTableDate = ref<any>([])

const purNoListParms = ref({
  lotNo: '',
  mtlNo: '',
  pageNum: 1,
  pageSize: 40,
  sortField: '',
  sortWay: '',
  sumNo: ''
})

const totalList1 = ref(0)

const materialsQuerryMethods = async () => {
  purNoListParms.value.lotNo = materialsQuerryLotNo.value
  purNoListParms.value.sumNo = materialsQuerryPrams.value
  isLoading2.value = true
  const result = await materialsQuerry(purNoListParms.value)
  materialsQuerryTableDate.value = result.lotNoList
  totalList1.value = result.total
  if (materialsQuerryTableDate.value.length > 0) {
    mtlNoList.value = materialsQuerryTableDate.value[0].mtlNo
    FitSubMaterialQueryMethods()
    FitSubMaterialQueryListMethods()
  }
  isLoading2.value = false
}

const Launchform = (row: any) => {
  SummaryInvoiceQueryParms.value.lotNo = row[0].inputValue
  QueryToBeSummarizedParms.value.lotNo = row[0].inputValue
}

const arrList = ref<any>([])

const findClicked = async (countText: string) => {
  switch (countText) {
    case '查询待汇总指令': {
      QueryToBeSummarizedMethods()
      break
    }
    case '查询已汇总指令': {
      SummaryInvoiceQueryMethods()
      break
    }
    case '不汇总指令删除': {
      change10()
      break
    }
    case '删除指令查询': {
      dialogVisible5.value = true
      QueryLotNoListMethods()
      break
    }
    case '汇总作业': {
      SummaryOperationMethods()
      break
    }
    case '导出汇总资料明细': {
      if (arrList.value.length !== 0) {
        const res = await fileRequest.post<any, any>(`/SummaryOperation/exportSummaryMaterialDetail`, arrList.value)
        const link = document.createElement('a') // 利用a标签自定义下载文件名
        let blob = new Blob([res.data]) // 创建Blob对象，设置文件类型
        link.style.display = 'none'
        //设置连接
        link.href = window.URL.createObjectURL(blob)
        link.download = `汇总明细资料导出${Date.now()}.xls` //自定义文件名字
        document.body.appendChild(link)
        //模拟点击事件
        link.click()
        document.body.removeChild(link)
      } else {
        JMessage.warning('请选择需要导出的数据')
      }
      break
    }
    case '删除汇总': {
      change11()
      break
    }
  }
}

const selectList1 = (row: any) => {
  arrList.value = row.map((item: any) => {
    return item.sumNo
  })
}

// 汇总号查询
const SummaryInvoiceQueryParms = ref({
  pageNum: 1,
  pageSize: 40,
  lotNo: ''
})

const handleSizeChange1 = (val: number) => {
  SummaryInvoiceQueryParms.value.pageSize = val
  SummaryInvoiceQueryMethods()
}
const handleCurrentChange1 = (val: number) => {
  SummaryInvoiceQueryParms.value.pageNum = val
  SummaryInvoiceQueryMethods()
}

const totalList = ref()

const SummaryInvoiceQueryTableDate = ref<any>([])

const SummaryInvoiceQueryMethods = async () => {
  isLoading1.value = true
  const result = await SummaryInvoiceQuery(SummaryInvoiceQueryParms.value)
  SummaryInvoiceQueryTableDate.value = result.lotNoList
  totalList.value = result.total
  if (SummaryInvoiceQueryTableDate.value.length > 0) {
    materialsQuerryPrams.value = SummaryInvoiceQueryTableDate.value[0].sumNo
    materialsQuerryMethods()
    QyeryLotNoMethods()
  }
  isLoading1.value = false
}

// 获取头部文字
const HeaderList = (a: any, b: any, c: any) => {
  if (a !== null) {
    QueryToBeSummarizedParms.value.sortField = b
    QueryToBeSummarizedParms.value.sortWay = a === 'ascending' ? 0 : 1
    QueryToBeSummarizedMethods()
  }
}

// 待汇总指令查询
const QueryToBeSummarizedTableDate = ref([])

const QueryToBeSummarizedParms = ref({
  pageNum: 1,
  pageSize: 40,
  lotNo: '',
  sortField: 'deliveryDate',
  sortWay: 1
})

const QueryToBeSummarizedMethods = async () => {
  isLoading.value = true
  const result = await QueryToBeSummarized(QueryToBeSummarizedParms.value)
  QueryToBeSummarizedTableDate.value = result.lotNoList
  total.value = result.total
  isLoading.value = false
}

const handleSizeChange = (val: number) => {
  QueryToBeSummarizedParms.value.pageSize = val
  QueryToBeSummarizedMethods()
}
const handleCurrentChange = (val: number) => {
  QueryToBeSummarizedParms.value.pageNum = val
  QueryToBeSummarizedMethods()
}

const total = ref()

onMounted(() => {
  QueryToBeSummarizedMethods()

  SummaryInvoiceQueryMethods()
})

const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入指令号',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'button',
    checkboxLabel: '查询待汇总指令'
  },
  {
    type: 'button',
    checkboxLabel: '查询已汇总指令'
  },
  {
    type: 'button',
    checkboxLabel: '导出汇总资料明细'
  }
])

const tableHeader = ref([
  {
    label: '厂区',
    prop: 'lotFact'
  },
  {
    label: '指令',
    prop: 'lotNo',
    sortable: true
  },
  {
    label: '型体',
    prop: 'style',
    sortable: true
  },
  {
    label: '颜色',
    prop: 'color',
    sortable: true
  },
  {
    label: '指令类型',
    prop: 'lotType',
    sortable: true
  },
  {
    label: '指令单日期',
    prop: 'sysDate',
    sortable: true
  },
  {
    label: '交期',
    prop: 'deliveryDate',
    sortable: true
  }
])

const tableHeader2 = ref([
  {
    label: '汇总号',
    prop: 'sumNo'
  },
  {
    label: '指令',
    prop: 'lotNo'
  },
  {
    label: '汇总日期',
    prop: 'sysDate'
  }
])

const tableHeader3 = ref([
  {
    label: '物料编码',
    prop: 'mtlNo'
  },
  {
    label: '材料颜色',
    prop: 'mtlColor'
  },
  {
    label: '材料名称',
    prop: 'mtlName'
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    label: '用量',
    prop: 'ttlUsage'
  },
  {
    label: '预加量',
    prop: 'advanceUsage'
  }
])

const tableHeader4 = ref([
  {
    label: '指令',
    prop: 'lotNo'
  },
  {
    label: '型体',
    prop: 'style'
  },
  {
    label: '配色',
    prop: 'color'
  },
  {
    label: '指令类型',
    prop: 'lotType'
  },
  {
    label: '订单量',
    prop: 'orderQty'
  },
  {
    label: '预补数',
    prop: 'surplusQty'
  }
])

const tableHeader5 = ref([
  {
    label: '指令',
    prop: 'lotNo'
  },
  {
    label: '物料编码',
    prop: 'mtlNo'
  },
  {
    label: '材料颜色',
    prop: 'mtlColor'
  },
  {
    label: '材料名称',
    prop: 'mtlName'
  },
  {
    label: '规格',
    prop: 'mtlSpec'
  },
  {
    label: '单位',
    prop: 'mtlUnit'
  },
  {
    label: '用量',
    prop: 'ttlUsage'
  },
  {
    label: '预加量',
    prop: 'advanceUsage'
  }
])

const tableHeader6 = ref([
  {
    label: '指令',
    prop: 'lotNo'
  },
  {
    label: '指令类型',
    prop: 'lotType'
  },
  {
    label: '用量',
    prop: 'ttlUsage'
  },
  {
    label: '预加量',
    prop: 'advanceUsage'
  }
])

const tableHeader7 = ref([
  {
    label: '指令',
    prop: 'lotNo'
  }
])
</script>

<style lang="less" scoped>
.main {
  width: 100%;
  background-color: white;
}

.btns {
  display: flex;
  padding: 10px 35px 5px;
  justify-content: end;

  .bg {
    background-color: #e7e7e7;
    border: 1px solid #e7e7e7;
    color: #000000;
  }
}
</style>
